# -*- coding: utf-8 -*-
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# 文件：post_channel_fund
# 日期：2020-12-14
# 备注：“财货通”为通道项目，通道方上海容之科技集团有限公司
财货通动自主设计、研发、生产的智能科技硬件产品将手机、3C数码产品及衍生品进行动态质押，为新零售与供应链金融相关行业提供质押、监管等全面的服务，解决资金方对企业的货物监管及风控问题。   
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

import requests
from interfacetest.baseset.channel_fund_api import API_CHANNEL
from pycacho.cachobase.logger import Logger
from pycacho.cachobase.deal_json import dict_to_json
from pycacho.cachobase.deal_time import minus_date
from msydwork.md5_encrypt import md5_encrypt
from msydwork.config.config_channel_fund import w_url
from msydwork.data.mysql_channel_fund import get_order,get_orders


logger = Logger("post_channel_fund").get_log()
cur_day = minus_date(0,'s')
cur_time = minus_date(0,'q')


def get_sign(caseId,json_data,channel):
    if channel == 'IJFQ':
        secret_key = 'rsA8KESJkQ2A1E7xrT3SerLsx9v6RQJA8sfUjFeSjuJ88LPMLFq69DuxgNouTT7k'
    else:
        secret_key = 'jUeH4Jmc6mTVSPohiFvCCOkjertbNrMTv4tbdDkBEukO7bqr1CPFDAyXZu1tTl1D'
    keys = ['channelCd', 'channelType', 'productCd', 'serviceId', 'caseId']
    keys.sort()
    org_str = ''
    for i in range(len(keys)):
        if i == 0:
            org_str = org_str + keys[i] + '=' + caseId + '&'
        else:
            org_str = org_str + keys[i] + '=' + str(json_data['serviceHeader'][keys[i]]) + '&'
    return  md5_encrypt(org_str)

# 进件
def channel_apply(channel,product,i):
    fu_url = w_url + 'open/api/v1/gateway'
    headers = API_CHANNEL['channel_headers']
    headers['requestId'] = 'AP' + product + cur_day + '%03d'%(i)
    headers['method'] = 'open.api.apply.cht'
    json_data = API_CHANNEL['channel_apply']
    dict_to_json(json_data)
    req1 = requests.post(fu_url,json=json_data,headers=headers)
    data = req1.text
    logger.info("财货通进件接口响应信息：%s",data)


# 借款协议签署
def channel_loan(channel,product,i):
    fu_url = w_url + 'open/api/v1/gateway'
    headers = API_CHANNEL['channel_headers']
    headers['requestId'] = 'LO' + product + cur_time + '%03d'%(i)
    headers['method'] = 'open.api.loan.cht'
    json_data = API_CHANNEL['channel_loan']
    json_data['applyNo'] = get_order('AP' + product + cur_day + '%03d'%(i))
    dict_to_json(json_data)
    req1 = requests.post(fu_url,json=json_data,headers=headers)
    data = req1.text
    logger.info("财货通借款协议签署接口响应信息：%s",data)


# 借款协议签署
def channel_subrogation(channel,product):
    fu_url = w_url + 'open/api/v1/gateway'
    headers = API_CHANNEL['channel_headers']
    headers['requestId'] = 'SB' + product + cur_time
    headers['method'] = 'open.api.subrogation.cht'
    json_data = API_CHANNEL['channel_subrogation']
    json_data['applyNoList'] = get_orders()
    dict_to_json(json_data)
    req1 = requests.post(fu_url,json=json_data,headers=headers)
    data = req1.text
    logger.info("财货通债转协议签署接口响应信息：%s",data)


def batch_apply():
    for i in range(22):
        channel_apply(channel, product, i)
        channel_loan(channel, product, i)



if __name__ == '__main__':
    channel = 'CHT'
    product = 'CHT001'
    i = 2
    type = 2
    if type == 0:
        channel_apply(channel, product, i)
    elif type == 1:
        channel_loan(channel, product, i)
    elif type == 2:
        channel_subrogation(channel, product)
    else:
        batch_apply()